const getLocation = () => {
	let promise = new Promise((resolve,reject)=>{
		uni.getLocation({
			type: 'wgx84',
			success: (res) => {
				// console.log(res);
				let latitude = res.latitude
				let longitude = res.longitude
				let speed = res.speed
				uni.request({
					url: 'https://api.map.baidu.com/geocoder/v2/?ak=Vh0ALNzHjjEm5RP0Ie16dlBhZbdEQip9&location=' +
						res.latitude + ',' + res.longitude + '&output=json',
					data: {},
					header: {
						'Content-type': 'application/json'
					},
					success: (ops) => {
						// 所在地址
						// let province = ops.data.result.addressComponent.province // 省份
						// let city = ops.data.result.addressComponent.city // 城市
						// let district = ops.data.result.addressComponent.district // 区
						// let street = ops.data.result.addressComponent.street
						let city = ops.data.result.addressComponent.city
						uni.request({
							url:`http://localhost:18001/api/city/findByCityName?cityName=`+city,
							data: {},
							header: {
								'Content-type': 'application/json'
							},
							success(data) {
								getApp().globalData.city.cityId  = data.data.data.cityId
								getApp().globalData.city.cityName  = data.data.data.cityName
								console.log(getApp().globalData.city)
							}
						})
						resolve()
					},
					fail: function(err) {
						console.log(err);
						reject(err)
					}
				})
			},
			fail: (err) => {
				console.log(err, '用户点击了拒绝');
				reject(err)
			}
		});
	})
	return promise
}
const Toast = (options) =>{
	uni.showToast({
		icon:options.icon || 'none',
		title:options.title || '',
		duration:options.duration || 1500,
		mask:options.mask || false,
	})
}
const setTabBarCity = () => {
	console.log(getApp().globalData.city.cityName)
	setTimeout(function(){
	uni.setTabBarItem({
		index:1,
		text:getApp().globalData.city.cityName,
		iconPath:'/static/tabbar-icons/feeds.png',
		selectedIconPath:'/static/tabbar-icons/feeds_s.png'
	});
	},500)
	
}
const goeasyConnect = () =>{
		let token = uni.getStorageSync('token');
		let user = uni.getStorageSync('user');
		if (token != '') {
			this.goEasy.connect({
				id: user.id, //pubsub选填，im必填，最大长度60字符
				data: {
					"avatar": user.photo.photoUrl,
					"nickname": user.username
				}, //必须是一个对象，pubsub选填，im必填，最大长度300字符，用于上下线提醒和查询在线用户列表时，扩展更多的属性
				onSuccess: function() { //连接成功
					console.log("GoEasy connect successfully.") //连接成功
				},
				onFailed: function(error) { //连接失败
					console.log("Failed to connect GoEasy, code:" + error.code + ",error:" + error
						.content);
				},
				onProgress: function(attempts) { //连接或自动重连中
					console.log("GoEasy is connecting", attempts);
				}
			});
		}
}
module.exports ={
	getLocation,
	Toast,
	setTabBarCity
}